import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/login.vue'
// import store from '@/store'
import { getToken } from '@/utils/token'

Vue.use(VueRouter)

const whitePage = ['/login', '/register']

const appRoutes = [
  {
    path: '/app/GroovyIndex',
    name: 'GroovyIndex',
    component: () => import('../views/main/app/script/index.vue')
  },
  {
    path: '/app/swagger',
    name: 'swagger',
    component: () => import('../views/main/app/swagger/index.vue')
  },
  {
    path: '/app/codeUtils',
    name: 'codeUtils',
    component: () => import('../views/main/app/codeUtils/index.vue')
  }
]

const sysRoutes = [
  {
    path: '/sys/busdict',
    name: 'busdict',
    component: () => import('../views/main/sys/dict/index.vue')
  }, {
    path: '/sys/paramConfig',
    name: 'paramConfig',
    component: () => import('../views/main/sys/config/index.vue')
  }, {
    path: '/sys/theme',
    name: 'theme',
    component: () => import('../views/main/sys/theme/index.vue')
  }, {
    path: '/sys/user',
    name: 'user',
    component: () => import('../views/main/sys/user/index.vue')
  }
]

const deployRoutes = [
  {
    path: '/deploy/deployIndex',
    name: 'deployIndex',
    component: () => import('../views/main/deploy/deployIndex.vue')
  },
  {
    path: '/deploy/logIndex',
    name: 'logIndex',
    component: () => import('../views/main/deploy/logIndex.vue')
  },
  {
    path: '/deploy/sourceIndex',
    name: 'sourceIndex',
    component: () => import('../views/main/deploy/sourceIndex.vue')
  }
]

const routes = [
  {
    path: '/',
    redirect: '/admin'
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../views/register.vue')
  },
  {
    path: '/admin',
    name: 'admin',
    component: () => import('../views/admin.vue'),
    redirect: '/admin/home',
    children: [
      {
        path: '/admin/home',
        name: 'home',
        component: () => import('../views/main/home.vue')
      },
      ...sysRoutes,
      ...deployRoutes,
      ...appRoutes
    ]
  },
  {
    path: '*',
    name: 'error',
    component: () => import('../views/404.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (whitePage.includes(to.fullPath)) { // 白名单页面直接放行
    next()
  } else {
    // 判断是否登录【根据有没有userId】
    // if (store.state.user.userId) {
    if (getToken()) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
